

//js实现函数的重载
function createOverload(){
    //建立映射关系
    const fnmap = new Map();
    function overload(...args){
        const key = args.map(it=>typeof it).join(',');
        const fn = fnmap.get(key);
        if(!fn){
            throw new TypeError('没有找到对应的实现')
        }
       return fn.apply(this, args)
    }
    overload.addImpl = function(...args){
        //最后一项是一个函数 前面是形参
        const fn = args.pop()
        if(typeof fn !== "function"){
            throw new TypeError("最后一个参数必须是函数")
        } 
        const key = args.join(",");
        fnmap.set(key, fn);
    }
    return  overload;
}



const getUsers = createOverload()
getUsers.addImpl(()=>{
    console.log('查询所有用户')
})

const searchPage = ((page,size = 10)=>{
    console.log('按照页码查询用户')
})

getUsers.addImpl('number',searchPage)
getUsers.addImpl('number','number',searchPage)

getUsers.addImpl('string',(name)=>{
    console.log('按照姓名查询')
})

getUsers.addImpl('string','string',(name,sex)=>{
    console.log('按照姓名和性别查询')
})

getUsers.addImpl('string','number',(name,age)=>{
    console.log('按照姓名和年龄查询')
})

getUsers('赵玉洁',26)